package com.aljfce.business.profile.boundary;

import com.aljfce.business.calendar.control.CalendarManager;
import com.aljfce.business.entity.User;
import com.aljfce.business.profile.control.ProfileManager;
import com.aljfce.business.profile.exception.UserNotFoundException;
import java.io.IOException;
import java.io.Serializable;
import java.security.Principal;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.http.HttpServletRequest;
import net.fortuna.ical4j.data.ParserException;
import net.fortuna.ical4j.model.ValidationException;
import org.primefaces.model.StreamedContent;
import org.primefaces.model.UploadedFile;

/**
 *
 * @author Antonio
 */
@ManagedBean
@ViewScoped
public class EditProfile implements Serializable {
    
    private String email;
    private String name;
    private String surname;
    private String password;
    private String visibility;
    private StreamedContent file;
    private UploadedFile ufile;
    
    @Inject
    ProfileManager pm;
    
    @Inject
    CalendarManager cm;
    
    @PersistenceContext
    EntityManager em;

    public EditProfile(){
    }
    
    @PostConstruct
    public void findDatas(){
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        
        Principal principal = request.getUserPrincipal();
        
        User current=null;
        try {
            current = pm.getProfile(principal.getName());
        } catch (UserNotFoundException ex) {
            Logger.getLogger(EditProfile.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        email=current.getEmail();
        name=current.getName();
        surname=current.getSurname();
                
        if(cm.getCalendar(email).getVisibility())
            visibility="Public";
        else
            visibility="Private";
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getVisibility() {
        return visibility;
    }

    public void setVisibility(String visibility) {
        this.visibility = visibility;
    }
    
    public UploadedFile getUfile() {
        return ufile;
    }

    public void setUfile(UploadedFile ufile) throws IOException, ParserException {
        this.ufile = ufile;
        System.out.println("Uploaded right");
    }
    
    public void importUfile (){
        try {
            cm.importCalendar(this.ufile);
        } catch (IOException | ParserException ex) {
            Logger.getLogger(EditProfile.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public StreamedContent getFile() {
        try {
            if(this.file == null)
                this.file=cm.exportCalendar();
            
        } catch (ValidationException | ParserException ex) {
            Logger.getLogger(CalendarManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        return file;
    }

    public void setFile(StreamedContent file) {
        this.file = file;
    }
    //TODO ANTONIO USED?
    public void showEdit (){
        
    }
    //TODO ANTONIO USED?
    public void showEditVisibility(){
        
    }
    //TODO ANTONIO USED?
    public void showUnRegistration(){
        
    }
    
    public String submitUserData (){
        pm.saveProfile(name,surname,password);
        return "/user/edit?faces-redirect=true";
    }
    
    public String deleteAccount(){
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        
        Principal principal = request.getUserPrincipal();
        final String name = principal.getName();
        
        try {
            pm.deleteProfile(name);
        } catch (UserNotFoundException ex) {
            Logger.getLogger(EditProfile.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        request.getSession().invalidate();
        
        return "/index?faces-redirect=true";
    }
    
    public String abort() throws IOException{
        return "/user/index?faces-redirect=true";
    }
    //per il momento finti anche questi dovrebbero usare il calendar manager solo che per ora da problemi
    public void setPublic(){
        cm.setVisibility(true);
        this.visibility="Public";
    }
    //per il momento finti anche questi dovrebbero usare il calendar manager solo che per ora da problemi
    public void setPrivate(){
        cm.setVisibility(false);
        this.visibility="Private";
    }
    
}
